/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.business;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;

import org.vineatoum.business.abstraction.BusinessAbstract;
import org.vineatoum.business.configuration.CuveType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Cuve extends BusinessAbstract {
	public static List<Cuve> cuves = new ArrayList<Cuve>();
	
	@XmlElement
	private String name;
	@XmlElement
	private float size;
	@XmlIDREF
	@XmlElement
	private CuveType kind;
	@XmlElement
	private boolean chapeauFlottant;
	
	public Cuve() {
		cuves.add(this);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the size
	 */
	public float getSize() {
		return size;
	}

	/**
	 * @param size the size to set
	 */
	public void setSize(float size) {
		this.size = size;
	}

	/**
	 * @return the kind
	 */
	public CuveType getKind() {
		return kind;
	}

	/**
	 * @param kind the kind to set
	 */
	public void setKind(CuveType kind) {
		this.kind = kind;
	}

	/**
	 * @return the chapeauFlottant
	 */
	public boolean isChapeauFlottant() {
		return chapeauFlottant;
	}

	/**
	 * @param chapeauFlottant the chapeauFlottant to set
	 */
	public void setChapeauFlottant(boolean chapeauFlottant) {
		this.chapeauFlottant = chapeauFlottant;
	}

	@Override
	public Cuve getNewInstance() {
		return new Cuve();
	}

	@Override
	public Vector<String> getVectorColumns() {
		Vector<String> vector = new Vector<String>();
		vector.add("Nom");
		vector.add("Taille");
		vector.add("Type");
		vector.add("Chapeau Flottant");
		return vector;
	}

	@Override
	public Vector<Object> getVectorData() {
		DecimalFormat df = new DecimalFormat("########.##"); 
		
		Vector<Object> vector = new Vector<Object>();
		vector.add(name);
		vector.add(df.format(size));
		vector.add(kind);
		if (chapeauFlottant) {
			vector.add("Oui");	
		} else {
			vector.add("Non");
		}
		
		return vector;
	}

	@Override
	public void updateField(String field, Object value) {
		try {
			if (field.equals("Nom")) {
				this.name = (String) value;
			} else if (field.equals("Taille")) {
				this.size = (Float) value;
			} else if (field.equals("Type")) {
				this.kind = (CuveType) value;
			} else if (field.equals("Chapeau Flottant")) {
				this.chapeauFlottant = (Boolean) value;
			} 
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	public String toString() {
		return name + " (" + size + " hl)";
	}
}
